package com.example.kotlin.ui

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.kotlin.R
import com.example.kotlin.ui.widget.ExpandTextView
import com.example.kotlin.utils.FontsUtil


/**
 * @ClassName: CloudBoardAdapter
 * @Description: 云上看盘列表
 * @Author: chenmengjia
 * @Date: 2022/5/6 9:39
 */
class CloudPlateAdapter(data: List<CloudPlateEntity>) :
    BaseMultiItemQuickAdapter<CloudPlateEntity, BaseViewHolder>(data) {

    init {
        //热点解读
        addItemType(CloudPlateEntity.TYPE_HOT_READ, R.layout.item_cloud_plate_hot_read)
        //股票
        addItemType(CloudPlateEntity.TYPE_STOCK, R.layout.item_cloud_plate_stock)
        //个股情况
        addItemType(CloudPlateEntity.TYPE_SELF_STOCK, R.layout.item_cloud_plate_self_stock)
        //机构调研
        addItemType(
            CloudPlateEntity.TYPE_INSTITUTION_RESEARCH,
            R.layout.item_cloud_plate_institution_research
        )
        //研报观点
        addItemType(
            CloudPlateEntity.TYPE_RESEARCH_PAPER_POINT_VIEW,
            R.layout.item_cloud_plate_news_pointview
        )
        //资金流向
        addItemType(CloudPlateEntity.TYPE_CAPITAL_FLOW, R.layout.item_cloud_plate_fund_flow)
        //龙虎榜单
        addItemType(CloudPlateEntity.TYPE_TIGER_DRAGON_LIST, R.layout.item_cloud_plate_list)
        //上榜理由
        addItemType(CloudPlateEntity.TYPE_ON_LIST_REASON, R.layout.item_cloud_plate_onlist_reason)
        //走势预测
        addItemType(CloudPlateEntity.TYPE_TREND_PREDICT, R.layout.item_cloud_plate_trend_predict)

    }

    fun getItemInfo(itemType: Int): Pair<Int, CloudPlateEntity>? {
        val data = data
        var pair: Pair<Int, CloudPlateEntity>? = null
        for (i in data.indices) {
            val carEntity = data[i]
            if (carEntity!!.itemType == itemType) {
                pair = Pair(i, carEntity)
                break
            }
        }
        return pair
    }

    override fun convert(helper: BaseViewHolder, item: CloudPlateEntity) {
        when (helper.itemViewType) {
            //热点解读
            CloudPlateEntity.TYPE_HOT_READ -> {
                val view = helper.getView<TextView>(R.id.tv)
                view.text = "2wwww2"
            }
            //股票
            CloudPlateEntity.TYPE_STOCK -> {
                val stockName = helper.getView<TextView>(R.id.tv_stock_name)
                val stockCode = helper.getView<TextView>(R.id.tv_stock_code)
                val price = helper.getView<TextView>(R.id.tv_price)
                val rate = helper.getView<TextView>(R.id.tv_rate)
                stockName?.apply {

                }
                stockCode?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "601933"

                }
                price?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "5.09"
                }
                rate?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "9.94%"
                }

            }
            //个股情况
            CloudPlateEntity.TYPE_SELF_STOCK -> {
                val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
                val arrayList = ArrayList<String>()
                arrayList.add("")
                arrayList.add("")
                val adapter = Adapter(arrayList, R.layout.item_self_stock_info)
                recyclerView.adapter = adapter
                recyclerView.layoutManager = LinearLayoutManager(mContext)

            }
            //机构调研
            CloudPlateEntity.TYPE_INSTITUTION_RESEARCH -> {
                val tv = helper.getView<ExpandTextView>(R.id.epTv)
                val w: Int = ScreenUtils.getScreenWidth() - mContext.resources
                    .getDimensionPixelOffset(R.dimen.space_30)
                tv.initWidth(w)
                tv.isExpand = false
                tv.setCloseText(mContext.getString(R.string.fi))
                tv.maxLines = 2
                tv.setOnExpandChangeListener {

                }

            }
            //研报观点
            CloudPlateEntity.TYPE_RESEARCH_PAPER_POINT_VIEW -> {
                val tv = helper.getView<ExpandTextView>(R.id.epTv)
                val w: Int = ScreenUtils.getScreenWidth() - mContext.resources
                    .getDimensionPixelOffset(R.dimen.space_30)
                tv.initWidth(w)
                tv.isExpand = false
                tv.setCloseText(mContext.getString(R.string.fi))
                tv.maxLines = 2
                tv.setOnExpandChangeListener {

                }
            }
            //资金流向
            CloudPlateEntity.TYPE_CAPITAL_FLOW -> {
                val threeDayFund = helper.getView<TextView>(R.id.tv_3_day_fund)
                val fiveDayFund = helper.getView<TextView>(R.id.tv_5_day_fund)
                val twentyDayFund = helper.getView<TextView>(R.id.tv_20_day_fund)
                threeDayFund?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "3.44亿元"
                }
                fiveDayFund?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "5.44亿元"
                }
                twentyDayFund?.apply {
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                    text = "-3.44亿元"
                }
            }
            //龙虎榜单
            CloudPlateEntity.TYPE_TIGER_DRAGON_LIST -> {
                //字体设置
                val t1 = helper.getView<TextView>(R.id.tv_num_1).apply {
                    text = "4.15"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }
                val t2 = helper.getView<TextView>(R.id.tv_num_2).apply {
                    text = "-2.34%"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }
                val t3 = helper.getView<TextView>(R.id.tv_num_3).apply {
                    text = "3.45%"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }

            }
            //上榜理由
            CloudPlateEntity.TYPE_ON_LIST_REASON -> {
                val tvBuyIn = helper.getView<TextView>(R.id.tv_buy_in)
                val tvSoldOut = helper.getView<TextView>(R.id.tv_sold_out)
                val tvNet = helper.getView<TextView>(R.id.tv_net)
                tvBuyIn?.apply {
                    text = "63848.73"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }
                tvSoldOut?.apply {
                    text = "20239.60"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }
                tvNet?.apply {
                    text = "43609.15"
                    typeface = FontsUtil.getInstance(context).numTypefaceBold
                }
                //列表
                val recyclerView = helper.getView<RecyclerView>(R.id.RecyclerView)
                val tvExpand = helper.getView<TextView>(R.id.tv_expand)
                val arrayList = ArrayList<CloudPlateEntity>()
//
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_TITLE))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM).apply {
                    showDivider = true
                })

                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_TITLE))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM))
                arrayList.add(CloudPlateEntity(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM))
                val adapter = ListAdapter(arrayList)
                recyclerView.adapter = adapter
                recyclerView.layoutManager = LinearLayoutManager(mContext)
                tvExpand.setOnClickListener {
                    recyclerView.apply {
                        visibility = if (visibility == View.GONE) {
                            View.VISIBLE
                        } else {
                            View.GONE
                        }
                        //文案
                    }


                }

            }

            //走势预测
            CloudPlateEntity.TYPE_TREND_PREDICT -> {

            }
        }
    }

    class Adapter(dataList: List<String>, layoutId: Int) :
        BaseQuickAdapter<String, BaseViewHolder>(layoutId, dataList) {

        override fun convert(helper: BaseViewHolder?, item: String) {

        }

    }

    class ListAdapter(dataList: List<CloudPlateEntity>) :
        BaseMultiItemQuickAdapter<CloudPlateEntity, BaseViewHolder>(dataList) {
        init {
            addItemType(CloudPlateEntity.TYPE_ON_LIST_REASON_TITLE, R.layout.item_list_reason_title)
            addItemType(CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM, R.layout.item_list_reason)
        }

        override fun convert(helper: BaseViewHolder, item: CloudPlateEntity) {

            when (helper.itemViewType) {
                CloudPlateEntity.TYPE_ON_LIST_REASON_TITLE -> {

                }
                CloudPlateEntity.TYPE_ON_LIST_REASON_ITEM -> {

                    val view = helper.getView<View>(R.id.view_divider)
                    if (item.showDivider) {
                        view.visibility = View.VISIBLE
                    } else {
                        view.visibility = View.GONE
                    }

                }
            }

        }

    }
}